<template>
  <div class="container">
    <el-card>
    <!-- 面包屑 -->
    <el-breadcrumb v-if="show" separator-class="el-icon-arrow-right">
      <el-breadcrumb-item >学科管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{rowdata.subjectName}}</el-breadcrumb-item>
      <el-breadcrumb-item>标签管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 头部 -->
    <el-row>
      <el-col :span="22">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="标签名称">
              <el-input v-model.trim="formInline.tagName" placeholder="请输入标签名称"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="formInline.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-button  size="small" @click="clefn">清除</el-button>
            <el-button type="primary" size="small" @click="seekfn">搜索</el-button>
            <el-link type="primary" v-if="show" icon="el-icon-back" @click="$router.push({ path: 'list' })">返回学科</el-link>
        </el-form>

      </el-col>
      <el-col :span="2">

        <el-button type="success" class="btn" size="small" icon="el-icon-edit" @click="addclkfn">新增标签</el-button>
      </el-col>
    </el-row>
      <el-alert
        :title="`数据一共 ${counts} 条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 头部 -->

      <!-- 主体 -->
      <template>
        <el-table
          :data="tableData"
          :header-cell-style="{
            background: '#fafafa',
            'border-bottom': '3px solid #e8e8e8'
          }"
          style="width: 100%"
        >
          <el-table-column label="序号" align="center" type="index">
          </el-table-column>
          <el-table-column align="center" prop="subjectName" label="所属学科">
          </el-table-column>
          <el-table-column align="center" prop="tagName" label="标签名称">
          </el-table-column>
          <el-table-column align="center" prop="username" label="创建者">
          </el-table-column>
          <el-table-column align="center" label="创建日期">
            <template slot-scope="scope">
              {{ scope.row.addDate | addDate }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="状态">
            <template slot-scope="scope">
              {{ scope.row.state === 1 ? '已启用' : '已禁用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button type="text" @click="stopfn(scope.row)">
                {{ scope.row.state == 0 ? '启用' : '禁用' }}
              </el-button>
              <el-button
                :disabled="scope.row.state === 1"
                type="text"
                @click="updatafn(scope.row)"
                >修改</el-button
              >
              <el-button
                :disabled="scope.row.state === 1 || scope.row.totals !== 0"
                @click="delfn(scope.row)"
                type="text"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        v-if="!show"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="obj.page"
        background
        :page-sizes="[5, 10, 20, 50]"
        :page-size="obj.pagesize"
        layout="prev, pager, next, sizes,jumper"
        :total="counts"
      >
      </el-pagination>
      <!-- 主体 -->
    </el-card>
    <!-- 弹框 -->
    <el-dialog
      :title="isedit ? '修改目录' : '新增目录'"
      :visible.sync="dialogVisible"
      width="20%"
    >
      <tagsAdd
        v-if="dialogVisible"
        :dialogVisible.sync="dialogVisible"
        :isedit="isedit"
        :row="row"
        @success="success"
      ></tagsAdd>
    </el-dialog>
    <!-- 弹框 -->
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags.js'
import tagsAdd from '../components/tags-add.vue'
export default {
  components: {
    tagsAdd
  },
  data () {
    return {
      tableData: [],
      obj: {
        page: 1,
        pagesize: 10,
        tagName: '',
        state: null
      },
      obj1: {
        page: 1,
        pagesize: 10
      },
      counts: 0,
      formInline: {
        tagName: '',
        state: null
      },
      show: false,
      dialogVisible: false,
      isedit: false,
      row: {},
      rowdata: {},
      xinhao: false
    }
  },
  created () {
    this.getpath()
  },
  watch: {
    $route () {
      this.show = false
      // console.log(898)
      this.getupdate()
    }
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.obj.pagesize = val
      this.getupdate()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.obj.page = val
      this.getupdate()
    },
    // 用来接受传过来的row
    getpath () {
      const rowdata = this.$route.params.row
      // console.log(rowdata, 598)
      if (rowdata !== undefined) {
        this.show = true
        this.rowdata = rowdata
        this.getupdate()
      } else {
        this.show = false
        this.getupdate()
      }
    },
    // 获取数据列表
    async getupdate () {
      const res0 = await list(this.obj)
      // console.log(res.data, 89898)
      if (this.show) {
        const res = await list({ page: 1, pagesize: res0.data.counts })
        console.log(res.data.items, 80000)
        const arr = res.data.items
        const narr = []
        const newarr = arr.filter((item) => {
          // console.log(this.rowdata, 8989)
          if (item.subjectName === this.rowdata.subjectName) {
            return narr.push(item)
          }
        })
        this.tableData = newarr
        this.counts = newarr.length
      } else {
        this.tableData = res0.data.items
        this.counts = res0.data.counts
      }
    },
    // 点击搜索按钮
    seekfn () {
      // this.obj.state = this.formInline.state
      // this.obj.directoryName = this.formInline.directoryName
      if (this.show) {
        // console.log(this.formInline, 89898)
        this.obj = this.formInline
        this.show = true
        this.getupdate()
      } else {
        this.obj = this.formInline
        this.getupdate()
      }
    },
    // 点击操作中的删除
    delfn (row) {
      // console.log(row, 200)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row, row)
          if (this.tableData.length === 1) {
            this.obj.page--
            if (this.obj.page === 1) {
              this.obj.page = 1
            }
          }
          this.getupdate()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击新增标签按钮
    addclkfn () {
      this.isedit = false
      this.dialogVisible = true
    },
    // 点击操作中的修改
    updatafn (row) {
      this.isedit = true
      this.row = row
      this.dialogVisible = true
    },
    // 点击操作中的禁用启用
    async stopfn (row) {
      if (row.state === 0) {
        row.state = 1
      } else {
        row.state = 0
      }
      await changeState(row)
    },
    // 点击清除按钮
    clefn () {
      this.formInline = {
        tagName: '',
        state: null
      }
      this.obj = this.formInline
      console.log(this.obj)
      this.getupdate()
    },
    // 子向父操作
    success () {
      this.dialogVisible = false
      this.getupdate()
    }
  }
}
</script>
<style scoped lang="scss">
.container {
  margin: 10px 15px;
  .el-breadcrumb {
    margin-bottom: 20px;
  }
  .el-input {
    width: 200px;
  }
  .btn {
    margin: 0px 20px;
  }
  .sp {
    margin-right: 10px;
  }
  .el-alert {
    margin-top: 10px;
    margin-bottom: 20px;
  }
  .el-pagination {
    text-align: right;
    margin-top: 20px;
  }
   .el-link{
  float: right;
  margin-top: 10px;
  }
}
</style>
